package cate.game.play.util;

import cate.common.table.d.GDSkill;
import cate.common.util.XT;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;


/**
 * 根据配置检测指定Fighter是否满足条件
 */
public class FighterFilter {

	private static final String 检测概率 = "检测概率";

	private static final String 检测基准 = "检测基准";


	private static final String 包含职业 = "包含职业";
	private static final String 排除职业 = "排除职业";

	private static final String 包含阵营 = "包含阵营";
	private static final String 排除阵营 = "排除阵营";

	private static final String 包含buff标签 = "包含buff标签";
	private static final String 排除buff标签 = "排除buff标签";

	private static final String 包含技能标签 = "包含技能标签";
	private static final String 排除技能标签 = "排除技能标签";

	private static final String 包含buff类型 = "包含buff类型";
	private static final String 排除buff类型 = "包含buff类型";

	private static final String 包含负面状态 = "包含负面状态";
	private static final String 处于控制状态 = "处于控制状态";

	private static final String 位置 = "位置";

	private static final String 比较属性 = "比较属性";
	private static final String 需求属性 = "需求属性";

	private static final String 是否觉醒 = "是否觉醒";





	// 检测概率=10000
	// 检测基准=1
	// &职业=1#2&阵营=3#4
	// &包含buff标签=灼烧#中毒&排除buff标签=灼烧#中毒
	// &包含技能标签=攻击#伤害&排除技能标签=攻击#伤害
	// &包含buff类型=护盾#互斥护盾&排除buff类型=护盾#互斥护盾
	// &包含负面状态=1&处于控制状态=1
	// &位置=1#2#3
	// &比较属性=1:1  (旧版配置：比较属性=1&比较属性大小=1)
	// &需求属性=1:10000:1 (旧版配置：需求属性=1&需求属性值=1000&需求属性大小=1)
	// &是否觉醒


	public static boolean test(PlayCfgArgs arg, Fighter actor, Fighter target) {
		if (arg == null) {
			return true;
		}
		Integer chance = arg.getInt(检测概率);
		if (chance != null && !XT.chance(chance)) {
			return false;
		}
		Byte base = arg.getByte(检测基准);
		if (base == null) {
			return true;
		}
		Fighter baseFighter;
		Fighter opponent;
		if (base == GDSkill.Base.CASTER) {
			baseFighter = actor;
			opponent = target;
		} else {
			baseFighter = target;
			opponent = actor;
		}
		List<Byte> jobs = arg.getByteList(包含职业);
		if (XT.isNotBlank(jobs) && !jobs.contains(baseFighter.getJob())) {
			return false;
		}
		List<Byte> excludeJobs = arg.getByteList(排除职业);
		if (XT.isNotBlank(excludeJobs) && excludeJobs.contains(baseFighter.getJob())) {
			return false;
		}

		List<Byte> camps = arg.getByteList(包含阵营);
		if (XT.isNotBlank(camps) && !camps.contains(baseFighter.getCamp())) {
			return false;
		}
		List<Byte> excludeCamps = arg.getByteList(排除阵营);
		if (XT.isNotBlank(excludeCamps) && excludeCamps.contains(baseFighter.getCamp())) {
			return false;
		}

		List<String> buffTags = arg.getStrList(包含buff标签);
		if (XT.isNotBlank(buffTags) && !baseFighter.buff.containTags(buffTags)){
			return false;
		}
		List<String> excludeBuffTags = arg.getStrList(排除buff标签);
		if (XT.isNotBlank(excludeBuffTags) && baseFighter.buff.containTags(excludeBuffTags)) {
			return false;
		}
		List<String> skillTags = arg.getStrList(包含技能标签);
		if (XT.isNotBlank(skillTags) && !baseFighter.skill.containTag(skillTags)){
			return false;
		}

		List<String> excludeSkillTags = arg.getStrList(排除技能标签);
		if (XT.isNotBlank(excludeSkillTags) && baseFighter.skill.containTag(excludeSkillTags)) {
			return false;
		}

		List<String> buffTypes = arg.getStrList(包含buff类型);
		if (XT.isNotBlank(buffTypes) && !baseFighter.buff.containType(buffTypes)) {
			return false;
		}
		List<String> excludeBuffTypes = arg.getStrList(排除buff类型);
		if (XT.isNotBlank(excludeBuffTypes) && baseFighter.buff.containType(excludeBuffTypes)) {
			return false;
		}

		Boolean containBad = arg.getBoolean(包含负面状态);
		if (containBad != null && !containBad == baseFighter.buff.containBad()) {
			return false;
		}
		Boolean underCtrl = arg.getBoolean(处于控制状态);
		if (underCtrl != null && !underCtrl == baseFighter.buff.underCtrl()) {
			return false;
		}

		List<Byte> positions = arg.getByteList(位置);
		if (XT.isNotBlank(positions) && !positions.contains(baseFighter.getPos())) {
			return false;
		}

		String cpAttrStr = arg.get(比较属性);
		if (StringUtils.isNotBlank(cpAttrStr)) {
			String[] cpAttrArray = cpAttrStr.split(":");
			if (cpAttrArray.length >= 2) {
				int cpAttr = Integer.parseInt(cpAttrArray[0]);
				boolean cpAttrBig = Integer.parseInt(cpAttrArray[0]) > 0;
				double baseAttr = baseFighter.attr.getTotalValue(cpAttr);
				double opponentAttr = opponent.attr.getTotalValue(cpAttr);
				if (cpAttrBig) {
					if (baseAttr < opponentAttr) {
						return false;
					}
				} else {
					if (baseAttr > opponentAttr) {
						return false;
					}
				}
			}
		}

		String requireAttrStr = arg.get(需求属性);
		if(StringUtils.isNotBlank(requireAttrStr)){
			String[] requireAttrArray = requireAttrStr.split(":");
			if (requireAttrArray.length >= 3) {
				int requireAttr = Integer.parseInt(requireAttrArray[0]);
				double requireAttrValue = Double.parseDouble(requireAttrArray[1]);
				boolean requireAttrBig = Integer.parseInt(requireAttrArray[2])>0;
				double attrValue = baseFighter.attr.getTotalValue(requireAttr);
				if (requireAttrBig) {
					if (attrValue < requireAttrValue) {
						return false;
					}
				} else {
					if (attrValue > requireAttrValue) {
						return false;
					}
				}
			}
		}
		Boolean awaken = arg.getBoolean(是否觉醒, null);
		if(awaken!=null&&baseFighter.status.isAwaken()!=awaken){
			return false;
		}
		return true;
	}

}
